app.controller('bpmHistoryProcessController', ['$scope', 'httpWrapService', 'dialogs', '$state', '$stateParams','GridFactory',
        function($scope, httpWrapService, dialogs, $state, $stateParams,GridFactory) {

    var columnDefs = [
        {name: "流程实例ID", field: "processInstanceId",
            cellTemplate: '<a ui-sref="app.bpm.history.finsh.view({processInstanceId:{{COL_FIELD}} })">{{COL_FIELD}}</a>'
        },
        {name: "所属流程", field: "processDefinitionId", cellFilter : 'bpmManagerProcessDefinitionFilter : "name"'} ,
        {name: "流程定义ID", field: "processDefinitionId"} ,
        {name: "启动时间", field: "startTime"} ,
        {name: "流程启动人", field: "startUserId"} ,
        {name: "结束时间", field: "endTime"} ,
        {name: "父流程ID", field: "superProcessInstanceId",
            cellTemplate: '<a ui-sref="app.bpm.history.finsh.view({processInstanceId:{{COL_FIELD}} })">{{COL_FIELD}}</a>'
        },
        {name: "结束原因", field: "deleteReason"},
        {
            displayName: "",
            field: "id",
            enableFiltering: false,
            enableSorting: false,
            maxWidth: 90,
            cellTemplate: '<button class="btn btn-danger m-b-xs w-xs" ng-disabled="grid.appScope.Grid.deletes[\'{{COL_FIELD}}\']" ng-click="grid.appScope.delete(this)">删除</button>'
        }
    ];

    $scope.Grid = GridFactory.newInstance({
        url : "bpm/history/process/finished/list.do",
        columnDefs : columnDefs,
        gridOptions : {
            useExternalSorting : false,
            useExternalPagination : true,
            useExternalFiltering : false
        }
    });
    $scope.Grid.load();


    $scope.delete = function(target) {
        var id = target.$parent.row.entity.id;
        httpWrapService.defaultDelete('bpm/history/process/finished/delete/'+ id +'.do', {
        }, "确认删除历史流程实例？").then(function(){
            $scope.Grid.deleteRow(target.$parent.row.entity);
        });
    };
}]);

/**
 */
app.filter('bpmManagerProcessDefinitionFilter', ["$sessionStorage", "httpWrapService","customObjectFilterFactory", function($sessionStorage, httpWrapService, customObjectFilterFactory) {
    return function(input, filed){
        return customObjectFilterFactory.newInstance({
            param : {},
            getUrl : function(intput, filed){
                return 'bpm/processinstance/view/' +input+ '.do';
            },
            getKey : function(intput, filed){
                return 'bpmManagerProcessDefinitionFilter_' + input;
            }
        }).filter(input, filed);
    };
}]);
